package com.example.battle.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.battle.R;
import com.example.battle.activity.FansNameSearchActivity;
import com.example.battle.activity.Login;
import com.example.battle.entity.FansName;
import com.example.battle.ui.adapter.TextTagsAdapter;
import com.moxun.tagcloudlib.view.TagCloudView;

import java.util.List;

public class FansNameFragment extends Fragment {
    private SearchView searchView;
    private TagCloudView tagCloudView;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_fansname, container, false);
        tagCloudView = root.findViewById(R.id.tag_cloud);
        searchView = root.findViewById(R.id.serachview);
        // 这一块是背景颜色
        //tagCloudView.setBackgroundColor(Color.LTGRAY);

        // 用粉丝名画一个球
        TextTagsAdapter tagsAdapter;
        List<FansName> list = Login.getInstance().getAppDB().battleDao().getBallData();
        if (list != null){
            String[] data = new String[list.size()];
            for (int i = 0; i < data.length; i++){
                data[i] = list.get(i).getFansName();
            }
            tagsAdapter = new TextTagsAdapter(data);
        }else {
            tagsAdapter = new TextTagsAdapter(new String[14]);
        }
        tagCloudView.setAdapter(tagsAdapter);

        // 搜索框样式及监听
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent = new Intent(getActivity(), FansNameSearchActivity.class);
                intent.putExtra("fansName",query);
                startActivity(intent);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });


        return root;
    }


}
